/**
******************************************************************************************
* @file ConnectionState.c
* @brief Recover and alarm with the connection change.
* @date 14 avril 2015
* @author Benjamin Lancinot
*****************************************************************************************/
/* COPYRIGHT ****************************************************************************
*****************************************************************************************
* Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
*    list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************************/

#include "ConnectionState.h"

/********************************************************************************************
* PROTOTYPES
********************************************************************************************/

void ConnectionState_changeStatus(ConnectionState*, char *);

void ConnectionState_destroyUtilities(ConnectionState*);

void ConnectionState_destroy(ConnectionState**);

int ConnectionState_registerObserver(ConnectionState*, Observer*);

int ConnectionState_unregisterObserver(ConnectionState*, Observer*);

/*******************************************************************************************
* FUNCTIONS
********************************************************************************************/

/**
 * @brief Constructor
 * @return Pointer to a new Connection state
 **/
ConnectionState* ConnectionState_create(){
	ConnectionState* This = (ConnectionState *) malloc(sizeof(*This));

	This->changeStatus = ConnectionState_changeStatus;
        This->destroy = ConnectionState_destroy;
	This->subject = subjectNew(This, 6);
	This->registerObserver = ConnectionState_registerObserver;
	This->unregisterObserver = ConnectionState_unregisterObserver;

        This->connectionStatus = "NO    ";

	return This;
}


/**
 * @brief Save and alarm observer when a new nonnection statu is detected.
 * @param This Pointer to the wanted ConnectionState
 * @param newStatus Char sequences with the new value of the connection.
 **/
void ConnectionState_changeStatus(ConnectionState* This, char * newStatus) {
        This->connectionStatus = newStatus;
       	This->subject->notifyObservers(This->subject);
}


/**
 * @brief Destroy all of the components of ConnectionState
 * @param This Pointer to the wanted ConnectionState
 **/
void ConnectionState_destroyUtilities(ConnectionState* This){
	This->subject->destroy(This->subject);

}


/**
 * @brief Destructor
 * @param This Double pointer to the wanted ConnectionState
 **/
void ConnectionState_destroy(ConnectionState** This){
        ConnectionState_destroyUtilities(* This);
	if (NULL != *This) {
		free(*This);
		*This = NULL;
	}
}


/**
 * @brief Proxy method for Register an observer for the ConnectionState object.
 * @param This Pointer to the wanted ConnectionState
 * @param This Pointer to the wanted observer
 * @return The result of the development of the registration (0 = GOOD and 1 = NOT GOOD)
 **/
int ConnectionState_registerObserver(ConnectionState* This, Observer* observer) {
	return This->subject->registerObserver(This->subject, observer);
}

/**
 * @brief Proxy method for Unregister observer for the ConnectionState object.
 * @param This Pointer to the wanted ConnectionState
 * @param This Pointer to the wanted observer
 * @return The result of the development of the unregistration (0 = GOOD and 1 = NOT GOOD)
 **/
int ConnectionState_unregisterObserver(ConnectionState* This, Observer* observer) {
	return This->subject->unregisterObserver(This->subject, observer);
}
